package com.ruoyi.jxxwx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.jxxwx.domain.bo.JxxPatrolBo;
import com.ruoyi.jxxwx.domain.vo.JxxPatrolVo;
import com.ruoyi.jxxwx.domain.JxxPatrol;
import com.ruoyi.jxxwx.mapper.JxxPatrolMapper;
import com.ruoyi.jxxwx.service.IJxxPatrolService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 巡查记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-12
 */
@RequiredArgsConstructor
@Service
public class JxxPatrolServiceImpl implements IJxxPatrolService {

    private final JxxPatrolMapper baseMapper;

    /**
     * 查询巡查记录
     */
    @Override
    public JxxPatrolVo queryById(Long patrolId){
        return baseMapper.selectVoById(patrolId);
    }

    /**
     * 查询巡查记录列表
     */
    @Override
    public TableDataInfo<JxxPatrolVo> queryPageList(JxxPatrolBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JxxPatrol> lqw = buildQueryWrapper(bo);
        Page<JxxPatrolVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询巡查记录列表
     */
    @Override
    public List<JxxPatrolVo> queryList(JxxPatrolBo bo) {
        LambdaQueryWrapper<JxxPatrol> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<JxxPatrol> buildQueryWrapper(JxxPatrolBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<JxxPatrol> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPatrolTime() != null, JxxPatrol::getPatrolTime, bo.getPatrolTime());
        lqw.eq(bo.getPlaceId() != null, JxxPatrol::getPlaceId, bo.getPlaceId());
        lqw.like(StringUtils.isNotBlank(bo.getPatrolUserName()), JxxPatrol::getPatrolUserName, bo.getPatrolUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getPatrolDept()), JxxPatrol::getPatrolDept, bo.getPatrolDept());
        lqw.eq(StringUtils.isNotBlank(bo.getPatrolArea()), JxxPatrol::getPatrolArea, bo.getPatrolArea());
        lqw.eq(StringUtils.isNotBlank(bo.getPatrolPictureUrl()), JxxPatrol::getPatrolPictureUrl, bo.getPatrolPictureUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getPatrolResult()), JxxPatrol::getPatrolResult, bo.getPatrolResult());
        lqw.eq(StringUtils.isNotBlank(bo.getReserve()), JxxPatrol::getReserve, bo.getReserve());
        lqw.eq(StringUtils.isNotBlank(bo.getReserve2()), JxxPatrol::getReserve2, bo.getReserve2());
        lqw.eq(StringUtils.isNotBlank(bo.getReserve3()), JxxPatrol::getReserve3, bo.getReserve3());
        return lqw;
    }

    /**
     * 新增巡查记录
     */
    @Override
    public Boolean insertByBo(JxxPatrolBo bo) {
        JxxPatrol add = BeanUtil.toBean(bo, JxxPatrol.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPatrolId(add.getPatrolId());
        }
        return flag;
    }

    /**
     * 修改巡查记录
     */
    @Override
    public Boolean updateByBo(JxxPatrolBo bo) {
        JxxPatrol update = BeanUtil.toBean(bo, JxxPatrol.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JxxPatrol entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除巡查记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
